package actions;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.util.HashMap;

import javax.swing.JTextPane;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.Element;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyleContext;
import javax.swing.text.Utilities;

import document.AwesomeDocument;

import textArea.AwesomeTextPanel;

/**
 * Highlights the keywords in the line that the caret is in.
 * @author Brittin
 *
 */
public class SimpleSyntaxHighlightAction extends AwesomeKeyAction {

	public SimpleSyntaxHighlightAction() {
		super("SimpleSyntaxHighlightAction");
	}

	@Override
	public void actionPerformed(ActionEvent e) {

		// Get parent AwesomeTextPanel
		AwesomeTextPanel panel = this.getParentAwesomeTextPanel(e);
		AwesomeDocument doc = panel.getDocumentType();
		
		if (doc == null)
			return;

		HashMap<String, Color> dict = doc.getLibrary();
		if (dict.isEmpty())
			return;
		
		// Get the text pane
		JTextPane pane = (JTextPane) e.getSource();
		
		// Find the current line
		synchronized (pane) {
		int pos = pane.getCaretPosition();
		Element elem = Utilities.getParagraphElement(pane, pos);
		int start = elem.getStartOffset();
		int end = elem.getEndOffset();

		try {
			
			// Highlighting will be done word for word for the entire line the text was entered into.
			String line = pane.getText(start, end - start);
			//System.out.print("Words are: ");
			
			String[] words = line.split("[ \t\n.]");
			int offset = 0;
			for (int i = 0; i < words.length; i++) {
				
				Color color = dict.get(words[i]);
				
				
				//System.out.print("\"" + words[i] + "\" ");
				int beginindex = start + line.indexOf(words[i], offset);

				int endindex = beginindex + words[i].length();
				StyleContext sc = StyleContext.getDefaultStyleContext();
				AttributeSet aset = null;
				if (color != null) { // color the word
					//System.out.print("\n Coded: " + "\"" + words[i] + "\" " +
							//beginindex + " " + endindex);
					aset = sc.addAttribute(SimpleAttributeSet.EMPTY,
							StyleConstants.Foreground, color);
				} else { // make it standard black
					aset = sc.addAttribute(SimpleAttributeSet.EMPTY,
							StyleConstants.Foreground, Color.black);
				}
				
				// Account for braces
				if (!words[i].equals("}") || !words[i].equals("{")) {
					pane.getStyledDocument().setCharacterAttributes(beginindex,
							endindex-beginindex, aset, false);
					
				}
				offset += words[i].length();
				
			}
			//System.out.print("\n");
			
		} catch (BadLocationException e1) {
			// TODO Auto-generated catch block
			//System.out.println("BadLocationException");
			//e1.printStackTrace();
		}
		}
	}
	
	private String formatWord(String originalWord) {
		String word = originalWord;
		word = word.replace("(", "");
		word = word.replace(")", "");
		word = word.replace("[", "");
		word = word.replace("]", "");
		word = word.replace("{", "");
		word = word.replace("}", "");
		return word;
	}
}
